﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PlanoAulaHelper.Reports;

namespace PlanoAulaHelper
{
    public partial class ListPlanoAulaForm : FormEdicao
    {
        public ListPlanoAulaForm() : base(0)
        {
            InitializeComponent();
            CarregarGrid();
        }

        private void CarregarGrid()
        {
            using (dbContext dc = new dbContext())
            {
                planoAulaCabBindingSource.DataSource = from dados in dc.PlanoAulaCabs
                                                       orderby dados.Ate descending, dados.Universidade, dados.Curso, dados.Materia
                                                       select dados;
            }
        }
        
        private void toolStripButtonAdicionar_Click(object sender, EventArgs e)
        {
            new PlanoAulaCabForm(0).ExbirFormulario(this);
            CarregarGrid();
        }

        private void dgvItem_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (planoAulaCabBindingSource.Current != null)
            {
                int id = ((PlanoAulaCab)planoAulaCabBindingSource.Current).Id;
                new PlanoAulaCabForm(id).ExbirFormulario(this);
                CarregarGrid();
            }
        }

        private void toolStripButtonPlanoDeAula_Click(object sender, EventArgs e)
        {
            if (planoAulaCabBindingSource.Current != null)
            {
                int id = ((PlanoAulaCab)planoAulaCabBindingSource.Current).Id;
                new relPlanoAulaForm(id).ExbirFormulario(this);
                CarregarGrid();
            }
        }

        private void detalhesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            dgvItem_MouseDoubleClick(sender, null);
        }

        private void HabilitarBotao(bool Habilitar)
        {
            excluirToolStripMenuItem.Enabled = Habilitar;
            detalhesToolStripMenuItem.Enabled = Habilitar;
            toolStripButtonPlanoDeAula.Enabled = Habilitar;
        }
        private void dgvItem_SelectionChanged(object sender, EventArgs e)
        {
            HabilitarBotao( planoAulaCabBindingSource.Current != null );
        }

        private void excluirToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (planoAulaCabBindingSource.Current != null)
            {
                if (MessageBox.Show("Deseja excluir o item selecionado?", "Excluir", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    int id = ((PlanoAulaCab)planoAulaCabBindingSource.Current).Id;

                    using (dbContext db = new dbContext())
                    {
                        db.ExcluirPlanoAula(id);
                    }
                    MessageBox.Show("Item excluído com sucesso.");
                    CarregarGrid();
                }
            }
        }

        
    }
}
